package shuiyuqing.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.whu.administrator.ws.R;
import com.whu.administrator.ws.UIHandler;

import java.util.ArrayList;
import java.util.List;

import inter.Delegate;
import inter.ResponseStatus;
import shuiyuqing.adapter.ChooseStationAdapter;
import shuiyuqing.data.AllPoints;
import shuiyuqing.dto.STINFO;
import shuiyuqing.network.GetRTHydInfo;
import shuiyuqing.network.GetWeather;

/**
 * 水雨情实时信息
 * Created by Administrator on 2016/3/21.
 */
public class ShuiYuQingActivity extends UIHandler.BaseActivity {

    // 显示的特殊站点界面
    View v_flow;
    View v_capacity;
    View v_bottom_history;
    TextView tv_time;
    TextView tv_kurong;
    TextView tv_shuiwei;
    TextView tv_float;
    TextView tv_dayRain;
    TextView tv_zongRain;
    TextView tv_stType;
    TextView tv_votage;

    Button btn_weather;

    PullToRefreshScrollView pullToRefreshScrollView;


    Spinner sp_loc;
    List<STINFO> stinfos = new ArrayList<STINFO>();
    ChooseStationAdapter chooseStationAdapter = new ChooseStationAdapter();

    {
        chooseStationAdapter.setStations(stinfos);
    }


    GetRTHydInfo getRTHydInfo = new GetRTHydInfo();

    Delegate realData = new Delegate() {
        @Override
        public void onResponse(String response) {
            pullToRefreshScrollView.onRefreshComplete();
            if (!response.startsWith("{")) return;
            Gson gson = new Gson();
            GetRTHydInfo.ResponseRealTimeHyd responseRealTimeHyd = gson.fromJson(response, GetRTHydInfo.ResponseRealTimeHyd.class);
            if (responseRealTimeHyd.getStatus().equals(ResponseStatus.SUCCESS)) {
                getData(responseRealTimeHyd.getRtData());
            }
        }

        @Override
        public void onErrorResponse(VolleyError error) {
            pullToRefreshScrollView.onRefreshComplete();
        }
    };

    {
        getRTHydInfo.setDelegate(realData);
    }


    // 获取到实时数据
    private void getData(GetRTHydInfo.RTData rtData) {
        Double kurong = rtData.getCAPACITY();
        if (kurong != null) {
            tv_kurong.setText(kurong + "");
        }

        Double liuliang = rtData.getFLOW();
        if (liuliang != null) {
            tv_float.setText(liuliang + "");
        }

        Double dayrain = rtData.getDAYRAINFALL();
        if (dayrain != null) {
            tv_dayRain.setText(dayrain + "");
        }

        Double waterLevel = rtData.getWATERLEVEL();
        if (waterLevel != null) {
            tv_shuiwei.setText(waterLevel + "");
        }

        Double zongRain = rtData.getSUMRAINFALL();
        if (zongRain != null)
        {
            tv_zongRain.setText(zongRain+"");
        }

        Double votageD = rtData.getVOLTAGE();
        if (votageD != null)
        {
            tv_votage.setText(votageD+"");
        }

        String time = rtData.getSURVEYTIME();
        if (time != null)
        {
            tv_time.setText(time);
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shuiyuqing);
        initview();
        initdata();
    }

    // 初始化数据
    private void initdata() {

        AllPoints.setNotifyGet(new AllPoints.NotifyGet() {
            @Override
            public void get(AllPoints allPoints) {
                stinfos.clear();
                stinfos.addAll(allPoints.getStinfos());
                chooseStationAdapter.notifyDataSetChanged();
            }
        });

        AllPoints allPoint = AllPoints.getAllPoints();

        if (allPoint != null) {
            stinfos.clear();
            stinfos.addAll(allPoint.getStinfos());
            chooseStationAdapter.notifyDataSetChanged();
        }


    }


    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == v_bottom_history) {
                Intent intent = new Intent(ShuiYuQingActivity.this, HistoryActivity.class);
                ShuiYuQingActivity.this.startActivity(intent);
            } else if (v == btn_weather) {
                Intent intent = new Intent(ShuiYuQingActivity.this, WeatherActivity.class);
                ShuiYuQingActivity.this.startActivity(intent);
            }
        }
    };

    private void initview() {

        pullToRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.id_refreshscrolview);
        pullToRefreshScrollView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        pullToRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                getRTHydInfo.get();
            }
        });

        v_bottom_history = findViewById(R.id.id_bottom_history);
        v_bottom_history.setOnClickListener(onClickListener);

        tv_time = (TextView) findViewById(R.id.id_tv_time);
        tv_zongRain = (TextView) findViewById(R.id.id_tv_zongyuliang);
        tv_votage = (TextView) findViewById(R.id.id_tv_votage);
        tv_kurong = (TextView) findViewById(R.id.id_tv_kurong);
        tv_shuiwei = (TextView) findViewById(R.id.id_tv_shuiwei);
        tv_float = (TextView) findViewById(R.id.id_tv_liuliang);
        tv_dayRain = (TextView) findViewById(R.id.id_tv_riyuliang);

        tv_stType = (TextView) findViewById(R.id.id_tv_sttype);

        btn_weather = (Button) findViewById(R.id.id_btn_weather);
        btn_weather.setOnClickListener(onClickListener);

        v_flow = findViewById(R.id.id_v_flow);
        v_capacity = findViewById(R.id.id_v_capacity);

        sp_loc = (Spinner) findViewById(R.id.id_spinner_loc);
        sp_loc.setAdapter(chooseStationAdapter);
        sp_loc.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                select(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }


    public void clear() {
        tv_time.setText("--");
        tv_dayRain.setText("暂无");
        tv_float.setText("暂无");
        tv_kurong.setText("暂无");
        tv_shuiwei.setText("暂无");
        tv_votage.setText("--");
        tv_zongRain.setText("暂无");
    }

    // 选中站点
    private void select(int position) {
        // 清空界面数据
        clear();
        STINFO stinfo = stinfos.get(position);
        tv_stType.setText(stinfo.getSTTYPE());
        getRTHydInfo.setData(stinfo.getSTID() + "");
        getRTHydInfo.get();

        // 特殊站点
        v_flow.setVisibility(View.GONE);
        v_capacity.setVisibility(View.GONE);
        if (stinfo.getSTID() == 58)
        {
            v_flow.setVisibility(View.VISIBLE);
        }

        if (stinfo.getSTID() == 60)
        {
            v_capacity.setVisibility(View.VISIBLE);
        }
    }


}
